
//import java.io.BufferedInputStream;
//import java.io.BufferedOutputStream;
//import java.io.BufferedReader;
//import java.io.File;
//import java.io.FileInputStream;
//import java.io.FileNotFoundException;
//import java.io.FileOutputStream;
//import java.io.FileReader;
//import java.io.FileWriter;
//import java.io.IOException;
//import java.io.ObjectInputStream;
//import java.io.ObjectOutputStream;
//import java.io.PrintWriter;
import java.io.*;
import java.util.Scanner;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *1
 * @author Pe Bong
 */
public class BankTest {

    public static void main(String arg[]) {
        int choice = 0;
        Scanner input = new Scanner(System.in);
        Bank bank = new Bank();
        
        try {
            int i = 0;
            FileInputStream fis = new FileInputStream("Account.txt");
            ObjectInputStream ois = new ObjectInputStream(fis);
            while (true) {
                if (fis.available() > 0) {
                    bank.Acc[i++] = (Account)ois.readObject();
                } else {
                    break;
                }
            }

            ois.close();
            fis.close();

        } catch (Exception e) {
        }
        try {
            /*FileReader fis = new FileReader("LaccNo.txt");
            BufferedReader bis = new BufferedReader(fis);
            String no = bis.readLine();
            bank.lastAccNo = Integer.parseInt(no);*/

            FileInputStream fis = new FileInputStream("LaccNo.txt");
            BufferedInputStream bis = new BufferedInputStream(fis);

            bank.lastAccNo= bis.read();
            bis.close();
            fis.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
        do {
            System.out.println("  \n ***  Bank Account Tranferring System   ***   \n");
            System.out.println("1.Create new account ");
            System.out.println("2.Deposit");
            System.out.println("3.Withdraw");
            System.out.println("4.Display all");
            System.out.println("5.Exit");
            System.out.println("    ****************");
            System.out.println("Enter your choice : ");
            choice = input.nextInt();
            switch (choice) {
                case 1:
                    System.out.println("Last Account NO xem tren case 1: " + bank.lastAccNo);
                    Account account = new Account();
                    bank.createAcc(account);
                    break;
                case 2:
                  
                    bank.deposit();
                    break;
                case 3:
                    bank.withdraw();
                    break;
                case 4:
                    bank.display();
                    break;
                case 5:
                    try {
                        FileOutputStream fos = new FileOutputStream("Account.txt");
                        ObjectOutputStream oos = new ObjectOutputStream(fos);
                        System.out.println("Last Account NO: " + bank.lastAccNo);
                        for (int i = 0; i < bank.lastAccNo; i++) {
                            oos.writeObject(bank.Acc[i]);
                        }
                        oos.flush();
                        oos.close();
                    } catch (FileNotFoundException ex) {
                        System.out.println("The requested file can not be found");
                    } catch (IOException ex) {
                        System.out.println("An error occurs when writting the file");
                    } catch (Exception ex) {
                    }

                    try {
                        //FileWriter fos = new FileWriter("LaccNo.txt");
                        //PrintWriter bos = new PrintWriter(fos);
                        FileOutputStream fos = new FileOutputStream("LaccNo.txt");
                        BufferedOutputStream bos = new BufferedOutputStream(fos);
                        System.out.println("Last Account NO xem o vong cuoi: " + bank.lastAccNo);
                        //bos.println(bank.lastAccNo + "");
                        
                        bos.write(bank.lastAccNo);
                        bos.flush();
                        bos.close();
                    } catch (Exception e) {
                    }
            }
        } while (choice != 5);
    }
}
